/*
 * Copyright 2009 Manuel Carrasco MoÃ±ino. (manuel_carrasco at users.sourceforge.net) 
 * http://code.google.com/p/gwtupload
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.binhthuan.catechist.server;

import gwtupload.server.UploadAction;
import gwtupload.server.exceptions.UploadActionException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import com.binhthuan.catechist.server.util.FileUtils;
import com.binhthuan.catechist.server.util.URLUtils;
import com.binhthuan.catechist.server.util.preferences.SystemGlobals;

/**
 * This class sends by email, all the fields and files received by GWTUpload
 * servlet.
 * 
 * @author Manolo Carrasco MoÃ±ino
 * 
 */
public class UploadServlet extends UploadAction {

    private static final long serialVersionUID = 1L;

    Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
    /**
     * Maintain a list with received files and their content types.
     */
    Hashtable<String, File> receivedFiles = new Hashtable<String, File>();

    /**
     * Override executeAction to save the received files in a custom place and
     * delete this items from session.
     */
    @Override
    public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
        StringBuilder response = new StringBuilder();
        int cont = 0;
        File uploadFile = null;
        String relativePath = "";
        for (FileItem item : sessionFiles) {
            if (false == item.isFormField()) {
                cont++;
                try {
                    // / Create a new file based on the remote file name in the
                    // client
                    // String saveName =
                    // item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+",
                    // "_");
                    // File file =new File("/tmp/" + saveName);

                    // / Create a temporary file placed in /tmp (only works in
                    // unix)
                    // File file = File.createTempFile("upload-", ".bin", new
                    // File("/tmp"));

                    // / Create a temporary file placed in the default system
                    // temp folder
                    synchronized (tmpFileLock) {
                        uploadFile = generateFile("upload-", FileUtils.getFileExtension(item.getName()), SystemGlobals
                                .getApplicationUploadTempDir());
                    }
                    item.write(uploadFile);
                    relativePath = URLUtils.getRelativePath(uploadFile.getAbsolutePath(), SystemGlobals
                            .getApplicationPath());
                    System.out.println("Upload finish: " + relativePath);
                    // Save a list with the received files
                    receivedFiles.put(item.getFieldName(), uploadFile);
                    receivedContentTypes.put(item.getFieldName(), item.getContentType());

                    // Compose a xml message with the full file information
                    // which can be parsed in client side
                    response.append("<file-").append(cont).append("-field>").append(item.getFieldName()).append(
                            "</file-").append(cont).append("-field>\n");
                    response.append("<file-").append(cont).append("-name>").append(item.getName()).append("</file-")
                            .append(cont).append("-name>\n");
                    response.append("<file-").append(cont).append("-size>").append(item.getSize()).append("</file-")
                            .append(cont).append("-size>\n");
                    response.append("<file-").append(cont).append("-type>").append(item.getContentType()).append(
                            "</file-").append(cont).append("-type>\n");
                    response.append("<file-").append(cont).append("-url>").append(relativePath).append("</file-")
                            .append(cont).append("-url>\n");
                } catch (Exception e) {
                    throw new UploadActionException(e.getMessage());
                }
            }
        }

        // Remove files from session because we have a copy of them
        removeSessionFileItems(request);

        // Send information of the received files to the client.
        return "<response>\n" + response.toString() + "</response>\n";
    }

    /**
     * Get the content of an uploaded file.
     */
    @Override
    public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fieldName = request.getParameter(PARAM_SHOW);
        File f = receivedFiles.get(fieldName);
        System.out.println("Get file: " + fieldName + " - Real path: " + f.getAbsolutePath());
        if (f != null) {
            response.setContentType(receivedContentTypes.get(fieldName));
            FileInputStream is = new FileInputStream(f);
            copyFromInputStreamToOutputStream(is, response.getOutputStream());
            if (is != null) {
                is.close();
            }
        } else {
            renderXmlResponse(request, response, ERROR_ITEM_NOT_FOUND);
        }
    }

    /**
     * Remove a file when the user sends a delete request.
     */
    @Override
    public void removeItem(HttpServletRequest request, String fieldName) throws UploadActionException {
        File file = receivedFiles.get(fieldName);
        receivedFiles.remove(fieldName);
        receivedContentTypes.remove(fieldName);
        if (file != null) {
            file.delete();
        }
    }

    private static final Object tmpFileLock = new Object();

    private static int counter = -1; /* Protected by tmpFileLock */

    private static File generateFile(String prefix, String suffix, String parentDir) throws IOException {
        if (counter == -1) {
            counter = new Random().nextInt() & 0xffff;
        }
        counter++;
        return new File(parentDir, prefix + Integer.toString(counter) + suffix);
    }
}
